/**
 * 项目信息列表
 * @param {} config
 */
DC.front.ProjectGrid = function(config) {
	//选择模式
	var _sm = new Ext.grid.CheckboxSelectionModel();
	// 定义一个ColumnModel，表头
	var _cm = new Ext.grid.ColumnModel([
		//new Ext.grid.RowNumberer(),
		_sm,
    	{
    		header: 'Id',
        	dataIndex: 'id',
        	width: 20,
        	hidden: true,
        	sortable: true
    	},
        {
        	header: 'Project Name',
        	id : 'project_name',
        	dataIndex: 'projectName',
        	width: 200,
        	sortable: true
        },
        {
        	header: 'Project Type',
        	dataIndex: 'projectType',
        	width: 60,
        	sortable: true
        },
        {
        	header: 'CreatedDate',
        	dataIndex: 'createdDateAsString',
        	width: 120,
        	sortable: true
        }
	]);

    var _store = new Ext.data.Store({
		autoDestroy: true,
		autoLoad: true,
		baseParams : { 
			start:0,
			limit:10
		},
        proxy: new Ext.data.HttpProxy({
        	method : 'POST',
        	url:DC.url.GET_PROJECT_DETAIL_LIST
        }),
        reader: new Ext.data.JsonReader({
        	successProperty: 'success',
            totalProperty: 'total',
            root: 'datas'
        }, [
            {name: 'id'},
            {name: 'projectName'},
            {name: 'projectType'},
            {name: 'createdDateAsString'}
        ])
    });
    _store.load();
    
    
	        
	var _bbar = new Ext.PagingToolbar({//分页工具栏  
        pageSize: 10, //每页显示几条数据 
        store:_store,
        displayInfo: true,
        displayMsg:'There are {2} closed project and {0} to {1} records are viewed.',   
        emptyMsg: "There is not closed project."   
	});
	
	DC.front.ProjectGrid.superclass.constructor.call(this, Ext.apply({
        id: 'project_gridPanel',
        margins : '-1 -1 -1 -1',
        stripeRows: true,//显示斑马线
		cm:_cm,
		sm: _sm,
		store:_store,
		header: false,
    	loadMask: {msg: 'Loading...'},
		//autoExpandColumn: 'project_name', //让简介列填充空余的页面
		viewConfig: {
			forceFit: true //自动填满表格
			//emptyText: '[There is not closed project.]'
		},
		bbar: _bbar
    }, config ||{}));
	
	_sm.on("selectionchange", function() {
		if (_sm.getSelections().length >= 1) {
			Ext.getCmp('open_project_win_open_btn').enable();
		} else {
			Ext.getCmp('open_project_win_open_btn').disable();
		}
	});
};

Ext.extend(DC.front.ProjectGrid, Ext.grid.GridPanel);


DC.front.ProjectGridWin =  function(config) {
	var pgw = this;
	var projectGrid = new DC.front.ProjectGrid();
	
	DC.front.ProjectGridWin.superclass.constructor.call(this, Ext.apply({
		id: 'project_grid_win',
		title : 'Open Project Win',
		border : true,
        bodyBorder : true,
        modal : true,
        closeAction : 'close',
        layout : 'fit',
        width : 600,
        height : 300,
        frame : true,
		autoScroll : false,
		items : [projectGrid],
		buttons : [{
			text : 'Open',
			id : 'open_project_win_open_btn',
			disabled : true,
			handler :function(){
				var selectedRecodes = projectGrid.getSelectionModel().getSelections();
				if (selectedRecodes.length >= 1) {
					var aryID = new Array();
					for (var i = 0, len = selectedRecodes.length; i < len; i++) {
						aryID.push(selectedRecodes[i].data['id']);
					}
					pgw.openProject(aryID.join(','));
				}
			}
		}, {
			text : 'Cancle',
			handler :function(){
				pgw.close();
			}
		}]
    }, config ||{}));
};

Ext.extend(DC.front.ProjectGridWin, Ext.Window, {
	openProject : function(idStr) {
		var pgwin = this;
		DC.util.CommonAction.loadMask("Openning...");
		Ext.Ajax.request({
		    url : DC.url.OPEN_PROJECT,
		    method : 'POST',
		    params : {
		    	ids : idStr
		    },
		    success : function(response, options){
		    	DC.util.CommonAction.unMask();
		    	var res = Ext.util.JSON.decode(response.responseText);
		    	if (res.success) {
		    		pgwin.close();
		    		DC.obj.explorer_treePanel.reloadData();
		    	} else {
		    		Ext.example.msg('Prompt win', res.msg, 'error');
		    	}
		    },
		    failure : function(response, options){
            	Ext.example.msg('Verification Win', response.responseText, 'error');
		    }
	    });
	}
});